Imports System.Web.Security
Imports MIMS.WRS.Business
Imports MIMS.WRS.Data
Imports MIMS.WRS.Entities


Public Class ForgotYourPassword
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label

    Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub


    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        btnCancel.Attributes.Add("onclick", "javascript:window.close();return false;")
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            'Validate user 
            Dim bValidUser As Boolean = True
            Dim email As String = txtUsername.Text.Trim()
            Dim userObj As ShoppingUser = New ShoppingUser
            Try
                userObj = ShoppingListService.GetUSER(email)
                If (userObj Is Nothing) Then
                    bValidUser = False
                End If
            Catch ex As Exception
                bValidUser = False
            End Try
            If (Not bValidUser) Then
                lblErr.Text = "This email has not registered yet. Please try other."
                Return
            End If
            'Prepare data to send email for update user with reset password
            Dim resetPasswordNotificationObj As ResetPasswordNotification = New ResetPasswordNotification
            resetPasswordNotificationObj.CustomerEmail = email
            Dim strUserName As String = ""
            Try
                strUserName = RequestContext.CurrentUser.NAME
            Catch ex As Exception
                Try
                    strUserName = HttpContext.Current.User.Identity.Name
                Catch ex1 As Exception
                End Try
            End Try
            resetPasswordNotificationObj.UserName = strUserName
            resetPasswordNotificationObj.Password = Constant.RESET_PASSWORD
            resetPasswordNotificationObj.Send()

            'Update user's password with reset password
            userObj.Password = Utilities.HashPassword(Constant.RESET_PASSWORD)
            ShoppingListService.UpdateShoppingUser(userObj)

            Page.RegisterStartupScript("windowClose", "<script language='javascript'>windowCloseAndRefresh1();</script>")

        Catch ex As Exception
            lblErr.Text = ex.Message
        End Try
    End Sub
End Class
